//
//  UIImageView+CycleExtension.m
//  iOSHeartAnimation
//
//  Created by ddy on 2018/8/10.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "UIImageView+CycleExtension.h"

@implementation UIImageView (CycleExtension)
/**
 * 1.用UIGraphics配合UIBezierPath绘制圆形图片
 */
- (void)cycle_circleImageView {
    
    //开始绘制
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0);
    //画圆形
    [[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.bounds.size.width] addClip];
    [self drawRect:self.bounds];
    //获取裁截后的图片
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    //结束绘制
    UIGraphicsEndImageContext();
}
/**
 * 2.用CAShapeLayer配合UIBezierPath绘制圆形图片
 */
- (void)cycle_circleImageViewWithBezier {
    //获取路径
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.bounds.size];
    //shapeLayer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.bounds;
    shapeLayer.path = bezierPath.CGPath;
    //设置图片layer
    self.layer.mask = shapeLayer;
    
    
}

/**
 * 3.截取中间部分的图
 */
+ (void)cycle_getCenterImageContentView:(UIImageView *)imageView {
    
    [imageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    
    imageView.clipsToBounds = YES;
    
}
/**
*  4.根据url，宽度固定，等比例缩放高度
*/
+ (double)cycle_getImageHeightFromImageUrl:(NSString *)url equalWidth:(double)itemW {
      double itemH = 0;
      UIImage * image;
      NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
      image = [UIImage imageWithData:data];
     //根据image的比例来设置高度
      if (image.size.width) {
         itemH = image.size.height / image.size.width * itemW;
      } else {
          itemH = 0;
      }
    return itemH;
}




@end
